<?php
require_once("../config/config.php");
include_once(CLASS_PATH."Template.php");
require_once(DATA_PATH."table_dao.php");
	$tablename	= _GET("tn","keywords");
	$pagenum	= _GET("p",1);
  
  $keywords_model = array(
    "title" =>  "
        <th>オークション<br />ショッピング</th>
        <th>カテゴリー</th>
        <th>言語</th>
        <th>ヒット数</th>
        <th>入力キーワード</th>
        <th>翻訳(ja)</th>
        <th>翻訳(cn)</th>
        <th>翻訳(tw)</th>
        <th>翻訳(en)</th>
        <th>ステータス</th>
        <th>日付</th>
        <th>操作</th>
      ",
    "body"  => "
        <td style=\"display:none\">
          <input name=\"id\" value=\"data_Id\" />
        </td>
        <td><input name=\"type\" value=\"data_sType\" /></td>
        <td><input name=\"cate\" value=\"data_Category\" /></td>
        <td><input name=\"client\" value=\"data_Lang\" /></td>
        <td><input name=\"hits\" value=\"data_hits\" /></td>
        <td><input name=\"key\" value=\"data_Key\" /></td>
        <td><input name=\"jkey\" value=\"data_jp\" /></td>
        <td><input name=\"ckey\" value=\"data_cn\" /></td>
        <td><input name=\"tkey\" value=\"data_tw\" /></td>
        <td><input name=\"ekey\" value=\"data_en\" /></td>
        <td><input value=\"data_handleFlag\" /></td>
        <td><input value=\"data_createDate\" /></td>
      <td><input type=\"submit\" data-type=\"edit\" value=\"[編集]\"><input type=\"submit\" data-type=\"del\" value=\"[削除]\"></td>
      "
  );  
  $feedback_model = array(
    "title" =>  "
        <th>名前</th>
        <th>ログインID</th>
        <th>メール</th>
        <th>キーワード</th>
        <th>オークション<br />ショッピング</th>
        <th>ステータス</th>
        <th>日付</th>
        <th>満足していない場所</th>
        <th>操作</th>
      ",
    "body"  =>  "
        <td style=\"display:none\">
          <input name=\"id\" value=\"data_Id\" />
        </td>
        <td><input name=\"username\" value=\"data_f_name\" /></td>
        <td><input name=\"userid\" value=\"data_f_uid\" /></td>
        <td><input name=\"email\" value=\"data_f_email\" /></td>
        <td><input name=\"key\" value=\"data_f_keyword\" /></td>
        <td><input name=\"type\" value=\"data_f_type\" /></td>
        <td><input value=\"data_handleFlag\" /></td>
        <td><input value=\"data_f_inserttime\" /></td>
        <td><textarea name=\"message\">data_f_message</textarea></td>
        <td><input type=\"submit\" data-type=\"edit\" value=\"[編集]\"><input type=\"submit\" data-type=\"del\" value=\"[削除]\"></td>
      "
  );
  if($tablename=="keywords"){
    $tmp_model = $keywords_model;
  }else{
    $tmp_model = $feedback_model;
  }
  $manage = new manage($tablename);
  switch(_GET("op")){
    case "update":
      $manage->updata(_GET("id"));
      break;
    case "del":
      $manage->deldata(_GET("id"));
      break;
    case "insert":
      $manage->adddata();
      break;
    default:
      $dataArr = $manage->showdata($pagenum);
      showmanage($tablename,$dataArr,$tmp_model,$manage->allcount,$pagenum);
      break;
  }
function showmanage($tablename,$data,$model,$allcount,$pagenum){
  
  $template = new Template(false,false);

  $tmp_body = replacestr($data,checknull("body", $model));
  $repstr = array(
    "##title#"=>checknull("title", $model),
    "##body#"=>$tmp_body,
    "##tablename#"=>$tablename,
    "##allcount#"=>$allcount,
    "##pagenum#"=>$pagenum,
    );
  $template->Out("admin.html",$repstr);
  //print_r($data);
}
function replacestr($data,$model){
  $result=NULL;
  if(is_array($data)){
    foreach($data as $i=>$datai){
      $result_tmp=NULL;
      if(is_array($datai)){
        $result_tmp=$model;
        foreach($datai as $key=>$val){
          $result_tmp = str_replace("data_".$key,$val,$result_tmp);
        }
      }
      $result.="<tr>$result_tmp</tr>";
    }
  }
  return $result;
}
class manage{
  private $manage_dao,$Data_model;
	private $Data_feedback,$Data_keywords;
	public $allcount;
  public function manage($tablename){
    $this->defaultsetting($tablename);
  }
  public function showdata($page){
    $result = $this->manage_dao->get($this->Data_model,$page);
    $this->allcount = $this->manage_dao->GetCount();
    return $result;
  }
  public function updata($id){
    $this->Data_model["handleFlag"] = "1";
    return $this->manage_dao->updata($id,$this->Data_model);
  }
  public function deldata($id){
    return $this->manage_dao->del($id);
  }
  public function adddata(){
    $this->Data_model["handleFlag"] = "";
    return $this->manage_dao->add($this->Data_model);
  }
  private function defaultsetting($tablename){
    //$this->manage_dao = new table_dao($tablename,true);//debug
    $this->manage_dao = new table_dao($tablename);
    if($tablename=="keywords"){
      $this->Data_model = array(
        "Id"	=>	_GET("id"),
        "sType"	=>	_GET("type"),
        "Category"	=>	_GET("cate"),
        "Lang"	=>	_GET("client"),
        "hits"	=>	_GET("hits"),
        "createDate"	=>	_GET("date"),
        "handleFlag"	=>	"",
        "Key"	=>	_GET("key"),
        "jp"	=>	_GET("jkey"),
        "cn"	=>	_GET("ckey"),
        "tw"	=>	_GET("tkey"),
        "en"	=>	_GET("ekey"),
        );
    }else{
      $this->Data_model = array(
        "Id"		=>	_GET("id"),
        "f_name"	=>	_GET("username"),
        "f_uid"	=>	_GET("userid"),
        "f_email"	=>	_GET("email"),
        "f_keyword"	=>	_GET("key"),
        "f_type"	=>	_GET("type"),
        "f_message"	=>	_GET("message"),
        "f_inserttime"	=>	_GET("date"),
        "handleFlag"	=>	""
        );
    }
  }
}
?>